<template>
  <div class='container'>
    <div class='tab-list'>
      <!-- <van-tabs>
        <van-tab :title='tabList[0].name' />
      </van-tabs> -->
      <div class='list'>
        <img class="couImg" src="https://h5.yixiang.co/static/images/banner_coupon.png" alt="">
        <div v-for='item in couponList' :key='item.id' class='list-item'>
          <div class='list-item-hd' :style="[{ background: tabList[active] ? tabList[active].bg : '' }]">
            <div class='list-item-money'>
              <span class='list-item-price'>{{ item.couponPrice }}</span>
              <span class='list-item-price-unit'>元</span>
            </div>
            <div class="minPrice">
              满{{ item.useMinPrice }}元可用
            </div>
          </div>
          <div class='list-item-bd'>
            <div class='list-item-bd-inner'>
              <div class='list-item-bd-title'>{{ item.cname }}</div>
              <div class='list-item-bd-txt'>{{ item.startTime }}至{{ item.endTime }}</div>
              <div style="height:auto">
                <span class="noDie">·不可叠加使用</span>
                <van-button class="goBuy" round type='danger' size='small'
                  @click='reciveCoupon(item.id)'>领取</van-button>
              </div>
            </div>
          </div>
        </div>
      </div>

    </div>
  </div>
</template>

<script>
import { getAllCoupons, receiveCoupons, getCoupons } from '@/api/discount.js'

export default {
  data() {
    return {
      couponList: [],
      button: false,
      active: 0,
      tabList: [
        {
          name: '可领取',
          bg: 'linear-gradient(300deg, rgb(250, 122, 122), rgb(254, 49, 78))'
        }
      ],
      userCoupons: []
    }
  },
  created() {
    this.getCoupon()
    this.getUserCoupon()
  },
  methods: {
    getCoupon() {
      getAllCoupons().then(res => {
        for (var i = 0; i < res.data.length; i++) {
          this.couponList.push(res.data[i])
        }
      })
    },
    onPopupShow() {
      this.exchangeShow = true
    },
    reciveCoupon(id) {
      var data = { couponId: id }
      receiveCoupons(data).then(res => {
        if (res.status == 200) {
          this.$toast('领取成功')
        } else {
          this.$toast('领取失败')
        }
      })
    },
    getUserCoupon() {
      getCoupons().then(res => {
        if (res.status == 200) {
          for (var i = 1; i < res.data.length; i++) {
            this.userCoupons.push(res.data[i])
          }
        }
      })
    }
  }
}
</script>

<style lang='scss' scoped>
.tab-list {
  height: calc(50px + constant(safe-area-inset-top));
  height: calc(50px + env(safe-area-inset-top));

  .van-tabs {
    box-sizing: border-box;
    margin-bottom: 15px;
    z-index: 3;
    position: fixed;
    width: 100%;
    height: 50px;
    top: 0;
    left: 0;
    padding-top: constant(safe-area-inset-top);
    padding-top: env(safe-area-inset-top);
  }
}

.list {
  box-sizing: border-box;
  padding-top: 5px;
}

.list-item {
  box-sizing: border-box;
  height: 100px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 10px;
  margin-left: 15px;
  margin-right: 15px;
  background: var(--white);
  box-shadow: 0px 2px 4px 2px rgba(228, 228, 228, 0.5);

  &-hd {
    box-sizing: border-box;
    width: 34%;
    text-align: center;
    height: 100%;
    padding: 20px 0;
    font-size: 12px;
    color: var(--white);
  }

  &-money {
    margin-bottom: 5px;
    margin-top: 10px;
  }

  &-price {
    font-size: 30px;
    font-weight: bold;
    margin-left: 5px;

    &-unit {
      margin-left: 5px;
      font-size: 12px;
    }
  }

  &-bd {
    flex: 1;
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 20px 0;

    &-title {
      font-size: 16px;
      color: var(--gray-color-8);
      margin-bottom: 10px;
      text-align: center;
    }

    &-txt {
      font-size: 12px;
      margin-left: 10px;
      margin-top: 15px;
    }

    .noDie {
      font-size: 12px;
      margin-top: 50px;
      margin-left: -5px;
    }
  }
}

.goBuy {
  float: right;
  width: auto;
  height: 20px;
}

.couImg {
  width: 100%;
  height: 150px;
  margin-bottom: 10px;
}

.minPrice {
  margin: 0 auto;
  font-size: 12px;
  width: 80%;
  background-color: rgba(249, 209, 209, 0.1);
  margin-top: 5px;
  border-radius: 30px ;
}
</style>
